//
//  RankingListViewController.m
//  YanMusicPlayer
//
//  Created by 1564335 on 15-6-13.
//  Copyright (c) 2015年 胡岩 All rights reserved.
//

#import "RankingListViewController.h"
#import "RankingListView.h"
#import "RankMusicList.h"
#import "AppDelegate.h"
#import "CDSideBarController.h"

@interface RankingListViewController ()<UIScrollViewDelegate, rankingSendVaule, sendSingerTouch, HeaderViewSize>

@property(nonatomic, strong) UIScrollView *bgScrollView;
@property(nonatomic, strong) UISegmentedControl *segented;
@property(nonatomic, strong) UIView *bgView;
@property(nonatomic, strong) UIView *selectView;
@property(nonatomic, strong) UIView *backgroundView;
@property(nonatomic, strong) UIView *singerListView;

@property(nonatomic, strong) UILabel *titleLabel;

@property(nonatomic, assign) CGFloat currentX;

@property(nonatomic, assign) CGFloat currentY;

@property(nonatomic, assign) CGFloat totalDistance;

@property(nonatomic, strong) HearderView *hearderView;

@property(nonatomic, assign) CGFloat startY;

@property(nonatomic, assign) NSInteger theTime;

@end

@implementation RankingListViewController

- (void)sendBegin
{
    [[CDSideBarController sharedInstance] dismissMenu];
    [UIView animateWithDuration:0.8 animations:^{
        self.singerListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
    }];
    _startY = _hearderView.frame.origin.y;
}
- (void)sendHeaderViewSize:(CGFloat)height
{
    _totalDistance += height;
    _currentY = height;
    if ((_hearderView.frame.origin.y + height) < _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT) && _hearderView.frame.origin.y + height > 74 * (_HEIGHT / _DEFAULT_HEIGHT)) {
        _hearderView.center = CGPointMake(_hearderView.center.x, _hearderView.center.y + height);
        if (_totalDistance > 0 && _startY != _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)) {
            
            [HearderView shareInstance].isList = NO;
            [UIView animateWithDuration:0.8 animations:^{
                [HearderView shareInstance].listTableView.alpha = 0.0f;
                [HearderView shareInstance].titleImageView.alpha = 1.0f;
            }];
            
            _backgroundView.alpha = 0.6 - 0.5 * (_totalDistance / (_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)));
        } else if (_totalDistance > -(_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)) && _startY != 74 * (_HEIGHT / _DEFAULT_HEIGHT)) {
            _backgroundView.alpha = 0.6 * (-_totalDistance / (_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)));
        }
    }
    [self.view bringSubviewToFront:_hearderView];
}

- (void)sendOver
{
    if (_theTime == 0 && (int)_startY == (int)(_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)) && _totalDistance < 0) {
        _backgroundView.userInteractionEnabled = NO;
        _segented.userInteractionEnabled = NO;
        _theTime = 1;
    } else if (_theTime == 1 && (int)_startY == (int)(74 * (_HEIGHT / _DEFAULT_HEIGHT)) && _totalDistance > 0) {
        _backgroundView.userInteractionEnabled = YES;
        _segented.userInteractionEnabled = YES;
        _theTime = 0;
    }
    
    if (_currentY > 0) {
        [UIView animateWithDuration:0.8 animations:^{
            _hearderView.frame = CGRectMake(0, _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
            _backgroundView.alpha = 0.0;
        }];
    } else if (_currentY <= 0) {
        [UIView animateWithDuration:0.8 animations:^{
            _hearderView.frame = CGRectMake(0, 74 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
            _backgroundView.alpha = 0.7;
        }];
    }
    [self.view bringSubviewToFront:_hearderView];
    _totalDistance = 0;
    _currentY = 0;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [HearderView shareInstance].isSongListView = NO;
    _backgroundView.alpha = 0.0;
    _hearderView = [HearderView shareInstance];
    _hearderView.frame = CGRectMake(0, _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
    [self.view addSubview:_hearderView];
    _hearderView.HeaderViewDelegate = self;
    [self.view bringSubviewToFront:_hearderView];
    [HearderView shareInstance].title.alpha = 0;
    [HearderView shareInstance].titleImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
    [HearderView shareInstance].titleImageView.center = CGPointMake(37 * (_WIDTH / _DEFAULT_WIDTH), 32 * (_HEIGHT / _DEFAULT_HEIGHT));
    
    [HearderView shareInstance].lastButton.frame = CGRectMake(_WIDTH - 240 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [HearderView shareInstance].startButton.frame = CGRectMake(_WIDTH - 160 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [HearderView shareInstance].nextButton.frame = CGRectMake(_WIDTH - 80 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    
    [HearderView shareInstance].progressSlider.alpha = 0;
    [HearderView shareInstance].likeBtn.alpha = 0;
    [HearderView shareInstance].downloadBtn.alpha = 0;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _WIDTH, _HEIGHT)];
    [self.view addSubview:backgroundImageView];
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *backgroundView = [[UIVisualEffectView alloc] initWithEffect:effect];
    backgroundView.frame = CGRectMake(0, 0, _WIDTH, _HEIGHT);
    [self.view addSubview:backgroundView];
    [backgroundImageView setImage:[UIImage imageNamed:@"bgImage2"]];
    
    // 初始化背景
    _backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    _backgroundView.backgroundColor = [UIColor blackColor];
    _backgroundView.alpha = 0.0;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [_backgroundView addGestureRecognizer:tap];
    [self.view addSubview:_backgroundView];
    
    _hearderView = [HearderView shareInstance];
    _hearderView.frame = CGRectMake(0, _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
    [self.view addSubview:_hearderView];
    _hearderView.HeaderViewDelegate = self;
    [self.view bringSubviewToFront:_hearderView];
    
    // 歌手列表
    self.singerListView = [[UIView alloc] initWithFrame:CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT))];
    self.singerListView.layer.cornerRadius = 5;
    self.singerListView.layer.masksToBounds = YES;
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.singerListView];
    self.singerListView.backgroundColor = [UIColor colorWithRed:211/255.0 green:218/255.0 blue:252/255.0 alpha:1];
    
    self.view.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 94 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT - 94 * (_HEIGHT / _DEFAULT_HEIGHT))];
    [self.view addSubview:_bgScrollView];
    _bgScrollView.delegate = self;
    _bgScrollView.contentSize = CGSizeMake(_WIDTH * 4, _HEIGHT - 94 * (_HEIGHT / _DEFAULT_HEIGHT));
    _bgScrollView.pagingEnabled = YES;
    _bgScrollView.backgroundColor = [UIColor clearColor];
    for (int i = 1; i < 5; i++) {
        RankingListView *rankingListView = [[RankingListView alloc] initWithFrame:CGRectMake(_WIDTH * (i - 1), 0, _WIDTH, _HEIGHT - 168 * (_HEIGHT / _DEFAULT_HEIGHT)) rank_Id:[NSString stringWithFormat:@"%d", i]];
        rankingListView.rankDelegate = self;
        [_bgScrollView addSubview:rankingListView];
    }
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(60 *(_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 120 *(_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT))];
    [self.view addSubview:_bgView];
    _bgView.backgroundColor = [UIColor whiteColor];
    
    _selectView = [[UIView alloc] initWithFrame:CGRectMake(60 *(_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT), (_WIDTH - 120 *(_WIDTH / _DEFAULT_WIDTH)) / 4, 30 * (_HEIGHT / _DEFAULT_HEIGHT))];
    _selectView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_selectView];
    
    NSArray *titleArray = [NSArray arrayWithObjects:@"综 合", @"华 语", @"欧 美", @"日 韩", nil];
    _segented = [[UISegmentedControl alloc] initWithItems:titleArray];
    _segented.frame = CGRectMake(60 *(_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 120 *(_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [self.view addSubview:_segented];
    _segented.tintColor = [UIColor clearColor];
    _segented.backgroundColor = [UIColor clearColor];
    _segented.selectedSegmentIndex = 0;
    [_segented addTarget:self action:@selector(changeMove:) forControlEvents:UIControlEventValueChanged];
    
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],
                                             NSForegroundColorAttributeName: [UIColor whiteColor]};
    [_segented setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName: [UIColor blackColor]};
    [_segented setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    [self.view bringSubviewToFront:_backgroundView];
     [self.view bringSubviewToFront:_singerListView];
    [self.view bringSubviewToFront:_hearderView];
}

- (void)selectIndex:(NSInteger)selectIndex selectId:(NSString *)selectId selectTitle:(NSString *)selectTitle
{
    [[CDSideBarController sharedInstance] dismissMenu];
    [UIView animateWithDuration:0.8 animations:^{
       _singerListView.frame = CGRectMake(0, 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        _backgroundView.alpha = 0.7;
    }];
    
    RankMusicList *rangkMusicList = [[RankMusicList alloc] initWithFrame:CGRectMake(0, 40 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 350 * (_HEIGHT / _DEFAULT_HEIGHT)) selectId:selectId isRank:YES];
    rangkMusicList.touchTableViewDelegate = self;
    rangkMusicList.backgroundColor = [UIColor clearColor];
    
    for (UIView *view in self.singerListView.subviews) {
        [view removeFromSuperview];
    }
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.singerListView.frame.size.width - 10, 40 * (_HEIGHT / _DEFAULT_HEIGHT))];
    self.titleLabel.textColor = [UIColor blackColor];
    [self.singerListView addSubview:self.titleLabel];
    [self.singerListView addSubview:rangkMusicList];
    self.titleLabel.text = selectTitle;
    
    
    [UIView animateWithDuration:0.8 animations:^{
        self.singerListView.frame = CGRectMake(0, 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        _backgroundView.alpha = 0.7;
    }];
    [self.view bringSubviewToFront:_backgroundView];
    [self.view bringSubviewToFront:self.singerListView];
    [self.view bringSubviewToFront:_hearderView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger number = scrollView.contentOffset.x / _WIDTH;
    [UIView animateWithDuration:0.8 animations:^{
        _segented.selectedSegmentIndex = number;
        self.selectView.frame = CGRectMake(60 * (_WIDTH / _DEFAULT_WIDTH) + number * (_WIDTH - 120 * (_WIDTH / _DEFAULT_WIDTH)) / 4, 30 * (_HEIGHT / _DEFAULT_HEIGHT), (_WIDTH - 120 * (_WIDTH / _DEFAULT_WIDTH)) / 4, 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    }];
}


- (void)changeMove:(UISegmentedControl *)segment
{
    NSInteger number = segment.selectedSegmentIndex;
    [UIView animateWithDuration:0.8 animations:^{
        self.bgScrollView.contentOffset = CGPointMake(number * _WIDTH, 0);
        self.selectView.frame = CGRectMake(60 * (_WIDTH / _DEFAULT_WIDTH) + number * (_WIDTH - 120 * (_WIDTH / _DEFAULT_WIDTH)) / 4, 30 * (_HEIGHT / _DEFAULT_HEIGHT), (_WIDTH - 120 * (_WIDTH / _DEFAULT_WIDTH)) / 4, 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    }];
    
}

// SingerMusicList 代理方法

- (void)sendTableViewTouchEnd
{
    if (self.currentX < 0 && self.singerListView.frame.origin.x != 0) {
        [UIView animateWithDuration:0.8 animations:^{
            self.singerListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
            _backgroundView.alpha = 0.0;
        }];
    } else if (self.currentX >= 0 && self.singerListView.frame.origin.x != 0) {
        [UIView animateWithDuration:0.8 animations:^{
            self.singerListView.frame = CGRectMake(0, 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
            _backgroundView.alpha = 0.7;
        }];
    }
    
    
    
    _totalDistance = 0;
    self.currentX = 0;
}

- (void)getTableViewTouch:(CGFloat)pointX
{
    self.currentX = 0;
    _totalDistance += pointX;
    if (pointX < -5 || pointX > 5) {
        self.currentX = pointX;
    }
    if (self.singerListView.frame.origin.x + pointX <= 0 && -_totalDistance < (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH))) {
        self.singerListView.center = CGPointMake(self.singerListView.center.x + self.currentX, self.singerListView.center.y);
        if (_totalDistance < 0) {
            _backgroundView.alpha = 0.5 - 0.5 * (-_totalDistance / (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH)));
        } else if (_totalDistance < (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH))) {
            _backgroundView.alpha = 0.5 * (_totalDistance / (_WIDTH - 30 * (_WIDTH / _DEFAULT_WIDTH)));
        }
    }
}

- (void)goBackTableView:(NSString *)SingerName number:(NSInteger)number
{
    [MusicPlayer1 shareInstance].isDownLoad = NO;
    [MusicPlayer1 shareInstance].isDownLoadList = NO;
    [[CDSideBarController sharedInstance] dismissMenu];
    MusicModel *model = [[MusicPlayer1 shareInstance].list objectAtIndex:number];
    [MusicPlayer1 shareInstance].firstPlay = YES;
    [MusicPlayer1 shareInstance].currentMusic = number;
    [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL URLWithString:model.url]];
    [MusicPlayer1 shareInstance].isPlay = YES;
    [[HearderView shareInstance].startButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    [[MusicPlayer1 shareInstance] play];
    [[HearderView shareInstance].listTableView reloadData];
}



- (void)tap
{
    [[CDSideBarController sharedInstance] dismissMenu];
    [UIView animateWithDuration:0.8 animations:^{
        self.singerListView.frame = CGRectMake(-(_WIDTH - 20), 150 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT));
        _backgroundView.alpha = 0.0;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
